package cvi.com.reader.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

import cvi.com.reader.R;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.constant;
import cvi.com.reader.tool.PermissionManager;
import cvi.com.reader.tool.ToastUtils;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by WP on 2020/7/20.
 * 启动页
 */

public class StartupHomePageActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    private boolean isFirstIn;//是否是第一次安装
    private int num = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        checkWritePermission1();
        if (Hawk.contains("isFirstIn")) {
            isFirstIn = Hawk.get("isFirstIn");
            isFirstIn = false;
            Log.d("isFirstIn", isFirstIn + "1111111111");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isFirstIn) {
                        startActivity(new Intent(StartupHomePageActivity.this, GuideActivity.class));
                    } else {
                        startActivity(new Intent(StartupHomePageActivity.this, LoginActivityNew.class));
                    }
                    finish();
                }
            }, 2000);
        } else {
            Hawk.put("isFirstIn", true);
            isFirstIn = true;
        }
    }

    /**
     * 检查读写权限权限
     */
    @AfterPermissionGranted(constant.WRITE_PERMISSION_CODE1)
    private void checkWritePermission1() {
        if (!PermissionManager.checkPermission(this, constant.PERMS_WRITE1)) {
            PermissionManager.requestPermission(this, constant.WRITE_PERMISSION_TIP, constant.WRITE_PERMISSION_CODE1, constant.PERMS_WRITE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将请求结果传递EasyPermission库处理
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    /**
     * 请求权限成功
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.d("isFirstIn", isFirstIn + "1111111111");
        Toast.makeText(StartupHomePageActivity.this, "授权成功", Toast.LENGTH_LONG).show();
        Log.d("isFirstIn", isFirstIn + "1111111111");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFirstIn) {
                    startActivity(new Intent(StartupHomePageActivity.this, GuideActivity.class));
                } else {
                    startActivity(new Intent(StartupHomePageActivity.this, LoginActivityNew.class));
                }
                finish();
            }
        }, 2000);
    }

    /**
     * 请求权限失败
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Log.d("isFirstIn", isFirstIn + "2222222");
        Toast.makeText(StartupHomePageActivity.this, "用户授权失败", Toast.LENGTH_LONG).show();
        /**
         * 若是在权限弹窗中，用户勾选了'NEVER ASK AGAIN.'或者'不在提示'，且拒绝权限。
         * 这时候，需要跳转到设置界面去，让用户手动开启。
         */
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).build().show();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }
}
